package com.virtualCat.service.impl;

import com.virtualCat.common.Msg;
import com.virtualCat.dao.OrderDao;
import com.virtualCat.dao.UserDao;
import com.virtualCat.entity.Order;
import com.virtualCat.entity.User;
import com.virtualCat.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单业务层实现
 *
 * @author Y-Rainson
 * @create 2018-03-28 21:38
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    UserDao userDao;
    /**
     * 我的页面信息
     *
     */
    public Msg mineInfo(String openid) {
        Map mineInfo=new HashMap();
        //0:待发货  1：待收货  2：已收货
        int waitDeliverNum=orderDao.mineInfo(openid,0);
        int waitReceiveNum=orderDao.mineInfo(openid,1);
        int finishNum=orderDao.mineInfo(openid,2);
        User user=new User();
        user=userDao.findByOpenid(openid);
        int[] num = new int[]{waitDeliverNum,waitReceiveNum,finishNum};
        mineInfo.put("num",num);
        mineInfo.put("mine",user);
        return Msg.success().add("mineInfo",mineInfo);
    }

    /**
     * 查询各种订单状态的数量及订单列表
     *
     */
    public Msg queryOrderNumberAndList(String openid,int status) {
        Map orderAndList=new HashMap();
        List<Order> orderList=orderDao.findOrderByStatus(openid,status);
        int number=orderDao.queryOrderNumber(openid,status);
        orderAndList.put("number",number);
        orderAndList.put("orderList",orderList);
        if(number<=0&&orderList.size()<0){
            return Msg.fail().add("order_empty",11401);
        }
        return Msg.success().add("order_number",orderAndList);
    }



    /**
     * 查看订单详情
     *
     */
    public Msg queryOrderDetail(String orderId) {
        Order order=new Order();
        order=orderDao.findOrderByOrderid(orderId);
        if(order==null){
            return Msg.fail().add("order_empty",11403);
        }
        return Msg.success().add("order_list",order);
    }

    /**
     * 更新订单状态
     *
     */
    public Msg updateOrderStatus(String orderid) {
        int status=orderDao.updateOrder(orderid);
        if(status==0){
            return Msg.fail().add("update_fail",11403);
        }
        return  Msg.success();
    }
}
